/*
 * CMediaFileConvService.h
 *
 *  Created on: 2023年7月12日
 *      Author: terry
 */

#ifndef CMEDIAFILECONVSERVICE_H_
#define CMEDIAFILECONVSERVICE_H_

#include "UMediaFileConverter.h"
#include <memory>
#include <deque>
#include <mutex>
#include <string>
#include "TaskThread.h"


class CMediaFileConvService : public UMediaFileConvService
			, protected comn::TaskThread
			, protected UMediaFileConverterSink
{
public:
	CMediaFileConvService();
	virtual ~CMediaFileConvService();

	virtual bool open(int mode, UMediaFileConverterSink* sink);

	virtual void close();

	virtual bool isOpen();

	virtual bool add(const char* inputPath, const char* outputPath, int64_t id);

	virtual bool remove(const char* inputPath);

	virtual void removeAll();

	virtual size_t size();


protected:
	virtual void onConvert(UMediaFileConverter* converter, int state, int64_t time, double percent);

	virtual void onIdle();

protected:

	struct ConvTask
	{
		int64_t id;
		std::string	inPath;
		std::string	outPath;
	};

	typedef std::deque< ConvTask >	ConvTaskQueue;

protected:
	bool fetchConvTask(ConvTask& task);

	bool checkFetchConvTask(ConvTask& task);

	bool isConverting();

	void closeConverter();

	void startConv(ConvTask& task);

	void onConvEnd(UMediaFileConverter* converter);

protected:
	int	m_mode;
	UMediaFileConverterSink* m_sink;

	ConvTaskQueue	m_convTasks;
	std::mutex	m_convMutex;

	std::shared_ptr< UMediaFileConverter >	m_converter;


};

#endif /* CMEDIAFILECONVSERVICE_H_ */
